package com.fr.report.script.function;

import com.fr.report.script.Function;
import com.fr.report.script.Function.Type;
import com.fr.report.script.NormalFunction;
import com.fr.report.script.Primitive;
import com.fr.report.script.core.parser.OperationUtils;
import com.fr.util.Utils;

public class MOD extends NormalFunction
{
  public Object run(Object[] paramArrayOfObject)
  {
    if (paramArrayOfObject.length < 2)
      return Primitive.ERROR_NAME;
    int i = Utils.objectToNumber(paramArrayOfObject[0], false).intValue();
    int j = Utils.objectToNumber(paramArrayOfObject[1], false).intValue();
    if (j == 0)
      return OperationUtils.POSITIVE_INFINITY;
    int k = i % j;
    if (j > 0)
      return new Integer(Math.abs(k));
    return new Integer(-Math.abs(k));
  }

  public Function.Type getType()
  {
    return Function.MATH;
  }

  public String getCN()
  {
    return "MOD(number,divisor):返回两数相除的余数。结果的正负号与除数相同。\nnumber:为被除数。\ndivisor:为除数。\n示例:\nMOD(3, 2) 等于 1\nMOD(-3, 2) 等于 1\nMOD(3, -2) 等于 -1\nMOD(-3, -2) 等于 -1\n";
  }

  public String getEN()
  {
    return "MOD(number,divisor): Returns the remainder after number is divided by divisor. The result has the same sign as divisor.\nNumber is the number for which you want to find the remainder.\nDivisor is the number by which you want to divide number.\n\nExample:\n   MOD(3, 2) = 1\n   MOD(-3, 2) = 1\n   MOD(3, -2) = -1\n   MOD(-3, -2) = -1\n";
  }
}